package com.DreamTeam.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.DreamTeam.dao.IUserDao;
import com.DreamTeam.po.User;

public class UserDao implements IUserDao {
	private Connection conn = null;

	public UserDao(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doAdd(User user) {
		String sql = "insert into user (userName,password,permission) values (?,?,?)";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, user.getUserName());
			prsmt.setString(2, user.getPassword());
			prsmt.setInt(3, user.getPermission());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;

	}

	@Override
	public boolean doUpdate(User user) {
		String sql = "update user set userName=?,password=? where userName=?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, user.getUserName());
			prsmt.setString(2, user.getPassword());
			prsmt.setString(3, user.getUserName());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public boolean doDelete(User user) {
		String sql = "delete from user where userName = ?";
		PreparedStatement prsmt = null;
		int flag = 0;
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, user.getUserName());
			flag = prsmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	@Override
	public List<User> queryUserByColumn(String keyword,String columnName,String orderColumn,String model){
		String sql = "select * from user where "+columnName+" like ? order by "+orderColumn+" "+model;
		PreparedStatement prsmt = null;
		List<User> list = new ArrayList<User>();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, "%"+keyword+"%");
			ResultSet re=prsmt.executeQuery();
			while(re.next()){
				User user = new User();
				user.setUserName(re.getString(1));
				user.setPassword(re.getString(2));
				user.setPassword(re.getString(3));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public List<User> queryUserAll() {
		String sql = "select * from user";
		PreparedStatement prsmt = null;
		List<User> list = new ArrayList<User>();
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				User user = new User();
				user.setUserName(re.getString(1));
				user.setPassword(re.getString(2));
				user.setPermission(re.getInt(3));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<User> queryOrderByColumnName(String columnName,String model) {
		String sql = "select * from user order by "+columnName+" "+model;
		PreparedStatement prsmt = null;
		List<User> list = new ArrayList<User>();
		try {
			prsmt = conn.prepareStatement(sql);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				User user = new User();
				user.setUserName(re.getString(1));
				user.setPassword(re.getString(2));
				user.setPermission(re.getInt(3));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public User queryUserByUserName(String userName) {
		String sql = "select * from user where userName=?";
		PreparedStatement prsmt = null;
		User user = new User();
		try {
			prsmt = conn.prepareStatement(sql);
			prsmt.setString(1, userName);
			ResultSet re = prsmt.executeQuery();
			while(re.next()){
				user.setUserName(re.getString(1));
				user.setPassword(re.getString(2));
				user.setPermission(re.getInt(3));
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}			
		return null;
	}
	
	

}
